package com.client.koshercook.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.user.client.rpc.IsSerializable;

/** saves fields of required information in 1 object*/
@PersistenceCapable
public class Globals implements IsSerializable{

	@PrimaryKey
    @Persistent
    private Key key; //the recipe key in data store

	@Persistent
    private int numOfRecipesInDS;
	
    @Persistent
    private int lastRecKipa; //the last scanned recipe from kipa
    
    @Persistent
    private Integer lastRecChefLavan; //the last scanned recipe from chef Lavan
  
    @Persistent
    private Integer lastRecRotev; //the last scanned recipe from rotev
    
    @Persistent
    private Integer lastRecStybel; //the last scanned recipe from Stybel
    
    @Persistent
    private Integer lastRecKosherCook; //the last recipe from our site, added by our users
    
    @Persistent
    private List<String> mostViewedRecipes; //5 most viewed recipes
    
    @Persistent
    private List<Integer> mostViewedNumber; //the number of views of the 5 most viewed recipes
    
    @Persistent
    private Integer weekNumber; //increased by 1 every week, for resetting recipes' numOfViews
    
    /** creates a new Globals object*/
    public Globals() {
    	this.key = KeyFactory.createKey(Globals.class.getSimpleName(), 1);
    	this.lastRecKipa = 0;
    	this.numOfRecipesInDS = 0;
    	this.setLastRecChefLavan(4);
    	this.setLastRecKosherCook(0);
    	this.setLastRecRotev(1);
    	this.setLastRecStybel(0);
 //   	System.out.println("init globals");
		weekNumber=0;
		mostViewedRecipes = new ArrayList<String>();
		mostViewedRecipes.add("בלינצ'ס במילוי גבינה@http://www.kipa.co.il/food/matkon/29");
		mostViewedRecipes.add("עוף בתפוזים@http://www.kipa.co.il/food/matkon/39");
		mostViewedRecipes.add("חמין ספגטי@http://www.kipa.co.il/food/matkon/5");
		mostViewedRecipes.add("עוף סיני@http://www.kipa.co.il/food/matkon/7");
		mostViewedRecipes.add("סלט כרוב סיני@http://www.kipa.co.il/food/matkon/18");
		mostViewedNumber = new ArrayList<Integer>();
		mostViewedNumber.add(0);
		mostViewedNumber.add(0);
		mostViewedNumber.add(0);
		mostViewedNumber.add(0);
		mostViewedNumber.add(0);
    }
    
    /**constructor to create a copy of a Globals object*/
    public Globals(Globals g) {
    	this.key = KeyFactory.createKey(Globals.class.getSimpleName(), 1);
    	this.lastRecKipa = g.getLastRecKipa();
    	this.numOfRecipesInDS = g.getNumOfRecipesInDS();
    	if(g.getLastRecChefLavan()==null) this.lastRecChefLavan = 4; //so we won't re-scan everything
    	else this.lastRecChefLavan = g.getLastRecChefLavan();
    	if(g.getLastRecRotev()==null) this.lastRecRotev = 1; //so we won't re-scan everything
    	else this.lastRecRotev = g.getLastRecRotev();
    	if(g.getLastRecStybel()==null) this.lastRecStybel = 0; //so we won't re-scan everything
    	else this.lastRecStybel = g.getLastRecStybel();
    	if(g.getLastRecKosherCook()==null) this.lastRecKosherCook = 0; //so we won't re-scan everything
    	else this.lastRecKosherCook = g.getLastRecKosherCook();
    	setWeekNumber(g.getWeekNumber());
    	if(g.getMostViewed()==null || g.getMostViewed().size()==0) initPopularRecipesArrays();
    	else {
	    	this.mostViewedRecipes = new ArrayList<String>();
	    	this.mostViewedRecipes.add(g.getMostViewed().get(0));
	    	this.mostViewedRecipes.add(g.getMostViewed().get(1));
	    	this.mostViewedRecipes.add(g.getMostViewed().get(2));
	    	this.mostViewedRecipes.add(g.getMostViewed().get(3));
	    	this.mostViewedRecipes.add(g.getMostViewed().get(4));
	    	this.mostViewedNumber = new ArrayList<Integer>();
	    	this.mostViewedNumber.add(g.getMostViewedNumber().get(0));
	    	this.mostViewedNumber.add(g.getMostViewedNumber().get(1));
	    	this.mostViewedNumber.add(g.getMostViewedNumber().get(2));
	    	this.mostViewedNumber.add(g.getMostViewedNumber().get(3));
	    	this.mostViewedNumber.add(g.getMostViewedNumber().get(4));
    	}
    }

	public Key getKey() {
		return key;
	}

	/** set the number of the last scanned recipe from site Kipa*/
	public void setLastRecKipa(int lastRecKipa) {
		this.lastRecKipa = lastRecKipa;
	}

	/** get the number of the last scanned recipe from site Kipa*/
	public int getLastRecKipa() {
		return lastRecKipa;
	}
    
	public void setLastRecChefLavan(int lastRecChefLavan) {
		this.lastRecChefLavan = lastRecChefLavan;
	}

	public Integer getLastRecChefLavan() {
		return lastRecChefLavan;
	}

	public void setLastRecRotev(int lastRecRotev) {
		this.lastRecRotev = lastRecRotev;
	}

	public Integer getLastRecRotev() {
		return lastRecRotev;
	}

	public void setLastRecStybel(Integer lastRecStybel) {
		this.lastRecStybel = lastRecStybel;
	}

	public Integer getLastRecStybel() {
		return lastRecStybel;
	}

	public void setLastRecKosherCook(Integer lastKosherCook) {
		this.lastRecKosherCook = lastKosherCook;
	}

	public Integer getLastRecKosherCook() {
		return lastRecKosherCook;
	}

	/**update all the fields of this object according to Globals g object*/
    public void updateFields(Globals g) {
    	setLastRecKipa(g.getLastRecKipa());
    	setLastRecChefLavan(g.getLastRecChefLavan());
    	setLastRecKosherCook(g.getLastRecKosherCook());
    	setLastRecRotev(g.getLastRecRotev());
    	setLastRecStybel(g.getLastRecStybel());
    	setNumOfRecipesInDS(g.getNumOfRecipesInDS());
    	setWeekNumber(g.getWeekNumber());
    	this.mostViewedRecipes.clear();
    	this.mostViewedRecipes.add(g.getMostViewed().get(0));
    	this.mostViewedRecipes.add(g.getMostViewed().get(1));
    	this.mostViewedRecipes.add(g.getMostViewed().get(2));
    	this.mostViewedRecipes.add(g.getMostViewed().get(3));
    	this.mostViewedRecipes.add(g.getMostViewed().get(4));
    	this.mostViewedNumber.clear();
    	this.mostViewedNumber.add(g.getMostViewedNumber().get(0));
    	this.mostViewedNumber.add(g.getMostViewedNumber().get(1));
    	this.mostViewedNumber.add(g.getMostViewedNumber().get(2));
    	this.mostViewedNumber.add(g.getMostViewedNumber().get(3));
    	this.mostViewedNumber.add(g.getMostViewedNumber().get(4));
    }
    
    public int getNumOfRecipesInDS() {
		return numOfRecipesInDS;
	}

	public void setNumOfRecipesInDS(int numOfRecipesInDS) {
		this.numOfRecipesInDS = numOfRecipesInDS;
	}

	public void setMostViewed(List<String> mostViewed) {
		this.mostViewedRecipes = mostViewed;
	}

	public List<String> getMostViewed() {
		return mostViewedRecipes;
	}

	public void setMostViewedNumber(List<Integer> mostViewedNumber) {
		this.mostViewedNumber = mostViewedNumber;
	}

	public List<Integer> getMostViewedNumber() {
		return mostViewedNumber;
	}

	public void setWeekNumber(Integer weekNumber) {
		this.weekNumber = weekNumber;
	}

	public Integer getWeekNumber() {
		return weekNumber;
	}
	
	/**
	 * check if recipe with this number of views is popular. if so, update the array.
	 * return if has changed
	 */
	public boolean checkAndUpdateViews(String recipe, int views) {
		System.out.println("globals: views="+views+" , num[0]="+mostViewedNumber.get(0));
		
		if(views<mostViewedNumber.get(4)) return false; //check if it's not gonna be popular
		if(mostViewedRecipes.get(0).equals(recipe)) { //check if it's already the most popular
			mostViewedNumber.set(0,views);
			return true;
		}
		boolean exist=false;
		for(int i=4 ; i>0 ; i--) { //check if recipes already popular
			if(mostViewedRecipes.get(i).equals(recipe)) { 
				exist=true;
				if(mostViewedNumber.get(i-1) < mostViewedNumber.get(i)) { //check if it's more popular now than the previously more popular recipe
					mostViewedRecipes.set(i,mostViewedRecipes.get(i-1));
					mostViewedNumber.set(i,mostViewedNumber.get(i-1));
					mostViewedRecipes.set(i-1,recipe);
					mostViewedNumber.set(i-1,views);
				}
				else {
					mostViewedNumber.set(i,views);
				}
			}
		}
		if(exist) return true;
		
		//if doesn't exist but it's popular, put it in the right place
		if(views<mostViewedNumber.get(3)) {
			mostViewedRecipes.set(4,recipe);
			mostViewedNumber.set(4,views);
		}
		else if(views<mostViewedNumber.get(2)) {
			mostViewedRecipes.set(4,mostViewedRecipes.get(3));
			mostViewedNumber.set(4,mostViewedNumber.get(3));
			mostViewedRecipes.set(3,recipe);
			mostViewedNumber.set(3,views);
		}
		else if(views<mostViewedNumber.get(1)) {
			mostViewedRecipes.set(4,mostViewedRecipes.get(3));
			mostViewedNumber.set(4,mostViewedNumber.get(3));
			mostViewedRecipes.set(3,mostViewedRecipes.get(2));
			mostViewedNumber.set(3,mostViewedNumber.get(2));
			mostViewedRecipes.set(2,recipe);
			mostViewedNumber.set(2,views);
		}
		else if(views<mostViewedNumber.get(0)) {
			mostViewedRecipes.set(4,mostViewedRecipes.get(3));
			mostViewedNumber.set(4,mostViewedNumber.get(3));
			mostViewedRecipes.set(3,mostViewedRecipes.get(2));
			mostViewedNumber.set(3,mostViewedNumber.get(2));
			mostViewedRecipes.set(2,mostViewedRecipes.get(1));
			mostViewedNumber.set(2,mostViewedNumber.get(1));
			mostViewedRecipes.set(1,recipe);
			mostViewedNumber.set(1,views);
		}
		else {
			mostViewedRecipes.set(4,mostViewedRecipes.get(3));
			mostViewedNumber.set(4,mostViewedNumber.get(3));
			mostViewedRecipes.set(3,mostViewedRecipes.get(2));
			mostViewedNumber.set(3,mostViewedNumber.get(2));
			mostViewedRecipes.set(2,mostViewedRecipes.get(1));
			mostViewedNumber.set(2,mostViewedNumber.get(1));
			mostViewedRecipes.set(1,mostViewedRecipes.get(0));
			mostViewedNumber.set(1,mostViewedNumber.get(0));
			mostViewedRecipes.set(0,recipe);
			mostViewedNumber.set(0,views);
		}
		return true;
	}
	
	/**
	 * reset views and increase weekNumber
	 */
	public void resetViews() {
		weekNumber++;
		for(int i=0 ; i<5 ; i++)
			mostViewedNumber.set(i,0);
	}
	
	public void initPopularRecipesArrays() {
//		System.out.println("init popular");
		weekNumber=0;
		mostViewedRecipes = new ArrayList<String>();
		mostViewedRecipes.add("בלינצ'ס במילוי גבינה@http://www.kipa.co.il/food/matkon/29");
		mostViewedRecipes.add("עוף בתפוזים@http://www.kipa.co.il/food/matkon/39");
		mostViewedRecipes.add("חמין ספגטי@http://www.kipa.co.il/food/matkon/5");
		mostViewedRecipes.add("עוף סיני@http://www.kipa.co.il/food/matkon/7");
		mostViewedRecipes.add("סלט כרוב סיני@http://www.kipa.co.il/food/matkon/18");
		mostViewedNumber = new ArrayList<Integer>();
		mostViewedNumber.add(0);
		mostViewedNumber.add(0);
		mostViewedNumber.add(0);
		mostViewedNumber.add(0);
		mostViewedNumber.add(0);
	}
}
